package com.skillworld.webapp.model.department.rank;

public enum Rank 
{
    NOVICE(0,50),
    BEGINNER(5,100),
    INTERMEDIATE(10,200),
    MASTER(15,400),
    MASTER_COACH(20,800);
    
        // does not apply to the highest rank
    public final static int MAX_GRADE = 4;
    
    private final int baseLevel;
    private final int pointsPerGrade;
    
    
    private Rank(int baseLevel, int pointsPerGrade)
    {
        this.baseLevel = baseLevel;
        this.pointsPerGrade = pointsPerGrade;
    }

    
    public int getBaseLevel() 
    {
        return baseLevel;
    }

    public int getPointsPerGrade() 
    {
        return pointsPerGrade;
    }
    
    
    public Rank getNext() 
    {
        return (this.ordinal() < Rank.values().length - 1)
                ? Rank.values()[this.ordinal() + 1]
                : null;
    }
    
    
    public int getBasePoints()
    {   
        int  points = 0;
        for (Rank t : Rank.values())
        {
            if (t.ordinal() < this.ordinal())
            {
                points += (MAX_GRADE+1)*t.getBasePoints();
            }
        }
        return points;
    }
    
    public static Rank fromLevel(int level)
    {
        if (level >= Rank.MASTER_COACH.getBaseLevel())
        {
            return Rank.MASTER_COACH;
        }
        else if (level >= Rank.MASTER.getBaseLevel())
        {
            return Rank.MASTER;
        }
        else if (level >= Rank.INTERMEDIATE.getBaseLevel())
        {
            return Rank.INTERMEDIATE;
        }
        else if (level >= Rank.BEGINNER.getBaseLevel())
        {
            return Rank.BEGINNER;
        }
        else
        {
            return Rank.NOVICE;
        }
    }
}
